package com.selfdriving.activity;

import java.util.ArrayList;
import com.selfdriving.R;
import net.tsz.afinal.FinalDb;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;

public class GuideActivity extends BaseActivity {
	private ViewPager viewPager;
	private ArrayList<View> list;
	private ImageView imageView;
	private ImageView[] imageViews;
	private Button wel_start;
	FinalDb db = null;
	String phone = null;
	Intent intent = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = getLayoutInflater();
		list = new ArrayList<View>();
		View view =  inflater.inflate(R.layout.welcome_guide3,null);
		list.add(inflater.inflate(R.layout.welcome_guide, null));
		list.add(inflater.inflate(R.layout.welcome_guide1, null));
		list.add(inflater.inflate(R.layout.welcome_guide2, null));
		list.add(view);
		wel_start = (Button) view.findViewById(R.id.btnStart);
		wel_start.setOnClickListener(new OnClickListenerImp());
		imageViews = new ImageView[list.size()];
		ViewGroup main = (ViewGroup) inflater.inflate(R.layout.welcome_pager, null);
		// group是R.layou.main中的负责包裹小圆点的LinearLayout.
		ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup);

		viewPager = (ViewPager) main.findViewById(R.id.viewPager);

		for (int i = 0; i < list.size(); i++) {
			imageView = new ImageView(GuideActivity.this);
			imageView.setLayoutParams(new LayoutParams(18, 18));// 设置小圆点的宽高
			imageView.setPadding(35, 0, 35, 0); // 设置小圆点左上右下的间隔
			imageViews[i] = imageView;
			if (i == 0) {
				// 默认进入程序后第一张图片被选中;
				imageViews[i]
						.setBackgroundResource(R.drawable.dot_carousel_focused);
			} else {
				imageViews[i]
						.setBackgroundResource(R.drawable.dot_carousel_normal);
			}
			group.addView(imageView);
		}

		setContentView(main);
		intent = getIntent();
		viewPager.setAdapter(new MyAdapter());
		viewPager.setOnPageChangeListener(new MyListener());
	}

	public class OnClickListenerImp implements OnClickListener {
		@Override
		public void onClick(View v) {
			shortcutDialog();
		}
	}

	private void shortcutDialog() {
		Dialog dialog = new AlertDialog.Builder(GuideActivity.this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle(R.string.shortcut)
				.setMessage(R.string.is_create)
				.setPositiveButton(R.string.create,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								addShortcuts();
								Intent intent = new Intent(
										GuideActivity.this,
										HomePageActivity.class);
								startActivity(intent);
								GuideActivity.this.finish();
							}
						})
				.setNegativeButton(R.string.Cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent intent = new Intent(
										GuideActivity.this,
										HomePageActivity.class);
								startActivity(intent);
								GuideActivity.this.finish();
							}
						}).create();
		dialog.show();
	}

	private void addShortcuts() {
		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");

		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.app_name));
		shortcut.putExtra("duplicate", false); // 不允许重复创建

		/**************************** 此方法已失效 *************************/
		// ComponentName comp = new ComponentName(this.getPackageName(),
		// "."+this.getLocalClassName());
		// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new
		// Intent(Intent.ACTION_MAIN).setComponent(comp)); 　　
		/****************************** end *******************************/
		Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
		shortcutIntent.setClass(this, Welcome.class);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

		// 快捷方式的图标
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
				this, R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

		sendBroadcast(shortcut);
	}

	class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(list.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(list.get(arg1));
			return list.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}
	}

	class MyListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[arg0]
						.setBackgroundResource(R.drawable.dot_carousel_focused);
				if (arg0 != i) {
					imageViews[i]
							.setBackgroundResource(R.drawable.dot_carousel_normal);
				}
			}
		}
	}
}
